package com.example.rjxmgl.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.example.rjxmgl.entity.dos.Leave;
import com.example.rjxmgl.entity.dos.User;
import com.example.rjxmgl.mapper.LeaveMapper;
import com.example.rjxmgl.service.LeaveService;
import com.example.rjxmgl.utils.ResultMessage;
import com.example.rjxmgl.utils.ResultUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.UUID;

/**
 * 请假表
 *
 * @author khai 951992121@qq.com
 * @since 1.0.0 2024-12-05
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService{

    @Override
    public ResultMessage studentAskForLeave(Leave leave) {

        User student = Db.query(User.class).eq("id", leave.getStudentId()).one();
        leave.setStatus(0);
        leave.setHeadTeacherId(student.getHeadTeacherId());
        leave.setCounselorId(student.getCounselorId());

        Db.save(leave);
        return ResultUtil.success();
    }

    @Override
    public ResultMessage headTeacherGetList(Long headTeacherId) {
        List<Leave> list = Db.query(Leave.class).eq("head_teacher_id", headTeacherId).list();
        return ResultUtil.data(list);
    }

    @Override
    public ResultMessage headTeacherCheck(Long leaveId, boolean result) {

        Leave le = Db.query(Leave.class).eq("id", leaveId).one();
        if(result){
            le.setStatus(1);
        }else {
            le.setStatus(3);
        }
        Db.updateById(le);
        return ResultUtil.success();
    }

    @Override
    public ResultMessage CounselorGetList(Long counselorId) {
        List<Leave> list = Db.query(Leave.class).eq("counselor_id", counselorId).eq("status",1).list();
        return ResultUtil.data(list);
    }

    @Override
    public ResultMessage counselorCheck(Long leaveId, boolean result) {
        Leave le = Db.query(Leave.class).eq("id", leaveId).one();
        if(result){
            le.setStatus(2);
        }else {
            le.setStatus(3);
        }
        Db.updateById(le);
        return ResultUtil.success();
    }

    @Override
    public ResultMessage studentGetList(Long studentId) {
        List<Leave> list = Db.query(Leave.class).eq("student_id", studentId).list();
        return ResultUtil.data(list);
    }
}